import requests
from time import sleep
from datetime import time


# 获取股票数据
def getStockData(stock_code):
    headers = {"referer": "http://finance.sina.com.cn"}
    page = requests.get(
        "http://hq.sinajs.cn/?format=text&list=" + stock_code,
        headers=headers,
        timeout=5,
    )
    stock_data = page.text
    return stock_data


def getTick(stock_code):
    stock = getStockData(stock_code)
    stock_field_list = stock.split(",")
    stock_info = stock_field_list[0].split("=")
    if len(stock_info) < 2 or stock_info[1] == "\n":
        return None

    tick = (
        stock_field_list[0],
        stock_field_list[3],
        stock_field_list[30] + " " + stock_field_list[31],
        stock_field_list[9],
    )
    return tick


# 数字转换
def str_of_num(num):
    """
    递归实现，精确为最大单位值 + 小数点后三位
    """

    def strofsize(num, level):
        if level >= 2:
            return num, level
        elif num >= 10000:
            num /= 10000
            level += 1
            return strofsize(num, level)
        else:
            return num, level

    units = ["", "万", "亿"]
    num, level = strofsize(num, 0)
    if level > len(units):
        level -= 1
    return "{}{}".format(round(num, 3), units[level])


# 打印股票信息
def printStockInfo(stock_code):
    stock_data = getStockData(stock_code)
    stock_data_list = stock_data.split(",")

    # 数据解构
    if len(stock_data_list) == 1:
        print(f"====标的：{stock_code}数据为空====")
    else:
        stock_info = stock_data_list[0].split("=")
        stock_code = stock_info[0]
        stock_name = stock_info[1]
        today_open = float(stock_data_list[1])
        yesterday_close = float(stock_data_list[2])
        current = float(stock_data_list[3])
        today_high = float(stock_data_list[4])
        today_low = float(stock_data_list[5])
        bid = float(stock_data_list[6])
        sell = float(stock_data_list[7])
        volume = round(float(stock_data_list[8]), 2)
        amount = float(stock_data_list[9])
        bid_1_v = float(stock_data_list[10])
        bid_1_p = float(stock_data_list[11])
        bid_2_v = float(stock_data_list[12])
        bid_2_p = float(stock_data_list[13])
        bid_3_v = float(stock_data_list[14])
        bid_3_p = float(stock_data_list[15])
        bid_4_v = float(stock_data_list[16])
        bid_4_p = float(stock_data_list[17])
        bid_5_v = float(stock_data_list[18])
        bid_5_p = float(stock_data_list[19])

        sell_1_v = float(stock_data_list[20])
        sell_1_p = float(stock_data_list[21])
        sell_2_v = float(stock_data_list[22])
        sell_2_p = float(stock_data_list[23])
        sell_3_v = float(stock_data_list[24])
        sell_3_p = float(stock_data_list[25])
        sell_4_v = float(stock_data_list[26])
        sell_4_p = float(stock_data_list[27])
        sell_5_v = float(stock_data_list[28])
        sell_5_p = float(stock_data_list[29])

        trade_date = stock_data_list[30] + " " + stock_data_list[31]

        print(
            f"编码：{stock_code}，名称：{stock_name}，开盘价：{today_open}，昨日收盘价：{yesterday_close}"
        )
        print(f"当前价格：{current}，今日最高：{today_high}，今日最低：{today_low}")
        print(f"竞买价：{bid}，竞卖价：{sell}")
        print(f"成交量：{(volume)}，成交额：{str_of_num(amount)}")
        print(
            f"买一：{bid_1_v}-{bid_1_p}，买二：{bid_2_v}-{bid_2_p}，买三：{bid_3_v}-{bid_3_p}，买四：{bid_4_v}-{bid_4_p} 买五：{bid_5_v}-{bid_5_p}"
        )
        print(
            f"卖一：{sell_1_v}-{sell_1_p}，卖二：{sell_2_v}-{sell_2_p}，卖三：{sell_3_v}-{sell_3_p}，卖四：{sell_4_v}-{sell_4_p}，卖五：{sell_5_v}-{sell_5_p}"
        )
        print(f"交易时间：{trade_date}")

        print(
            "=================================================================================="
        )
    pass


for i in range(1, 400):
    if i < 10:
        stock_code = "sz12309" + str(i)
        # printStockInfo("sz12300" + str(i))
    elif i >= 10 and i < 100:
        stock_code = "sz1230" + str(i)
    else:
        stock_code = "sz123" + str(i)
    
    tick = getTick(stock_code)
    if tick is not None:
        volume = tick[3]
        if float(volume) > 100000000:
            print(tick[0]+'_'+volume)
    else:
        pass
        # print(f'====标的信息为空{stock_code}====')

# while True:
#     last_tick = getStockData()
#     stock_info = last_tick.split(',')
#     name = stock_info[0]
#     today_open_price = stock_info[1]
#     print(f'债券信息:{name}\n 今日开盘价：{today_open_price}')
#     print(stock_info[29]+"_"+stock_info[31])
#     sleep(3)

# trade_time = time(9, 30)
# while time(9) < trade_time < time(15):
    # print(getTick("sz123093"))
    # sleep(3)
    # pass



